package com.heu.blood.dict.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import org.apache.logging.log4j.util.Strings;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.heu.blood.common.utils.PageUtils;
import com.heu.blood.common.utils.Query;

import com.heu.blood.dict.dao.DictDataDao;
import com.heu.blood.dict.entity.DictDataEntity;
import com.heu.blood.dict.service.DictDataService;


@Service("dictDataService")
public class DictDataServiceImpl extends ServiceImpl<DictDataDao, DictDataEntity> implements DictDataService {

    @Autowired
    private DictDataDao dictDataDao;

    /**
     * 分页查询字典数据项信息
     * @param params
     * @return
     */
    @Override
    public PageUtils queryPage(Map<String, Object> params) {
        // 构造DictDataEntity
        DictDataEntity dictDataEntity = new DictDataEntity();
        // 添加查询字段
        if (Strings.isNotBlank((String) params.get("dictType"))){
            dictDataEntity.setDictType((String) params.get("dictType")); // 字典类型
        }
        if (Strings.isNotBlank((String) params.get("dictCode"))){
            dictDataEntity.setDictCode((String) params.get("dictCode")); // 字典代码
        }
        if (Strings.isNotBlank((String) params.get("dictValue"))){
            dictDataEntity.setDictValue((String) params.get("dictValue")); //字典值
        }

        // 创建queryWrapper对象
        LambdaQueryWrapper<DictDataEntity> queryWrapper = new LambdaQueryWrapper<>();
        // 添加查询条件
        queryWrapper.eq(Strings.isNotBlank(dictDataEntity.getDictType()), DictDataEntity::getDictType, dictDataEntity.getDictType())
                    .eq(Strings.isNotBlank(dictDataEntity.getDictCode()), DictDataEntity::getDictCode, dictDataEntity.getDictCode())
                    .eq(Strings.isNotBlank(dictDataEntity.getDictValue()), DictDataEntity::getDictValue, dictDataEntity.getDictValue())
        ;

        IPage<DictDataEntity> page = this.page(
                new Query<DictDataEntity>().getPage(params),
                queryWrapper
        );

        return new PageUtils(page);
    }

    /**
     * 条件查询字典数据项信息
     * @param dictDataEntity
     * @return
     */
    @Override
    public List<DictDataEntity> query(DictDataEntity dictDataEntity) {
        // 创建queryWrapper对象
        LambdaQueryWrapper<DictDataEntity> queryWrapper = new LambdaQueryWrapper<>();
        // 添加查询条件
        queryWrapper.eq(Strings.isNotBlank(dictDataEntity.getDictType()), DictDataEntity::getDictType, dictDataEntity.getDictType())
                .eq(Strings.isNotBlank(dictDataEntity.getDictCode()), DictDataEntity::getDictCode, dictDataEntity.getDictCode())
                .eq(Strings.isNotBlank(dictDataEntity.getDictValue()), DictDataEntity::getDictValue, dictDataEntity.getDictValue());
        // 返回结果
        return dictDataDao.selectList(queryWrapper);
    }

    /**
     * 根据字典类型查询具体字典数据
     * @param dictType
     * @return
     */
    @Override
    public List<DictDataEntity> selectDictData(String dictType) {
        // 创建queryWrapper对象
        LambdaQueryWrapper<DictDataEntity> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(Strings.isNotBlank(dictType), DictDataEntity::getDictType, dictType);
        return dictDataDao.selectList(queryWrapper);
    }

    /**
     * 前端获得所有的dictData进行code和value的转换
     * @return
     */
    @Override
    public Map<String, Map<String, String[]>> getAllDictData() {
        List<DictDataEntity> list = this.list();
        Map<String, Map<String, String[]>> res = new HashMap<>();
        for (DictDataEntity dictDataEntity : list){
            String curType = dictDataEntity.getDictType();
            // 如果没有这个key，就新建一个
            if (!res.containsKey(curType)){
                res.put(curType, new HashMap<>());
            }
            if (Strings.isNotBlank(dictDataEntity.getDictAppendix())){
                // 如果字典附录不为空就加上
                res.get(curType).put(dictDataEntity.getDictCode(), new String[]{dictDataEntity.getDictValue(), dictDataEntity.getDictAppendix()});
                continue;
            }
            res.get(curType).put(dictDataEntity.getDictCode(), new String[]{dictDataEntity.getDictValue()});
        }
        return res;
    }

    /**
     * 根据code查询appendix
     * @param code
     * @return
     */
    @Override
    public DictDataEntity queryByCode(String code) {
        LambdaQueryWrapper<DictDataEntity> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(Strings.isNotBlank(code), DictDataEntity::getDictCode, code);
        return dictDataDao.selectOne(queryWrapper);
    }

    @Override
    public Map<String, String> getDictAndCode() {
        List<DictDataEntity> list = this.list();
        Map<String, String> res = new HashMap<>();
        for (DictDataEntity dictDataEntity : list){
            res.put(dictDataEntity.getDictValue(),dictDataEntity.getDictCode());
        }
        return res;
    }

    @Override
    public Map<String, String> getValToCode() {
        List<DictDataEntity> lists = this.list();
        Map<String, String> res = new HashMap<>();
        for(DictDataEntity dictDataEntity : lists){
            res.put(dictDataEntity.getDictValue(), dictDataEntity.getDictCode());
        }
        return res;
    }
}